/**
 * 
 */
package com.tw.trips.dao;

import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.neo4j.helpers.collection.IteratorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.neo4j.repository.GraphRepository;
import org.springframework.data.neo4j.template.Neo4jOperations;
import org.springframework.stereotype.Repository;
import org.springframework.test.context.ContextConfiguration;

import com.tw.trips.domain.Location;
import com.tw.trips.domain.Update;
import com.tw.trips.repository.LocationRepository;

/**
 * @author M1005264
 * 
 */
@Repository
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class LocationDAO {

	@Autowired
	private Neo4jOperations template;

	@Autowired
	private LocationRepository repository;

	// Dont load the updates eagerly here
	public List<Location> getAllLocations() {
		GraphRepository<Location> repo = template.repositoryFor(Location.class);
		Page<Location> pages = repo.findAll(new PageRequest(1, 10));
		return pages.getContent();
	}

	public Set<Update> getUpdates(Location reqLocation) {
		return template.findOne(reqLocation.getNodeiD(), Location.class)
				.getUpdates();
	}

	public Location save(Location reqLocation) {
		return template.save(reqLocation);
	}

	public Location getLocation(String fromLocation) {
		return repository.findByName(fromLocation);
	}

	public Collection<Location> findAllLocations() {
		return IteratorUtil.asCollection(repository.findAll());
	}

}
